package gui.specialGUIs;

import definitions.Commands;
import entities.Address;
import entities.Event;
import gui.designComponents.TMTComboBox;
import gui.designComponents.TMTDefaultFormBuilder;
import gui.designComponents.TMTNormalButton;
import gui.designComponents.TMTPanel;
import gui.designComponents.TMTTextField;
import gui.designComponents.TMTTitledBorder;

import java.awt.BorderLayout;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;

import util.Auth;
import app.Controller;
import app.Model;
import bundles.BundleLoader;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Panel to show and edit a single Event
 * 
 * @author MK
 * 
 */
public class EventSinglePanel extends TMTPanel {

	private TMTTextField yearField;
	private TMTComboBox monthBox;
	private TMTComboBox dayBox;
	private TMTComboBox timeBox;

	private TMTComboBox addressIdentifierBox;
	private TMTTextField streetField;
	private TMTTextField housenumberField;
	private TMTTextField postalField;
	private TMTTextField cityField;

	private Model model;
	private Controller controller;

	private ParticipantsOveriewPanel pop;
	private EventSingleButtonPanel buttonPanel;

	public EventSinglePanel(Model model, Controller controller) {

		this.model = model;
		this.controller = controller;

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		setLayout(new BorderLayout());

		monthBox = new TMTComboBox(model.getMonthValues());
		dayBox = new TMTComboBox(model.getDaysOfMonthValues());
		timeBox = new TMTComboBox(model.getTimeValues());
		yearField = new TMTTextField();
		streetField = new TMTTextField();
		housenumberField = new TMTTextField();
		postalField = new TMTTextField();
		cityField = new TMTTextField();
		addressIdentifierBox = new TMTComboBox(model.getAddressValues());
		addressIdentifierBox
				.setActionCommand(Commands.SHOW_EVENT_ADDRESS_CHANGE);

		FormLayout layout = new FormLayout(
				"right:max(40dlu;p), 4dlu, 60dlu, 5dlu, "
						+ "right:max(40dlu;p), 4dlu, 50dlu, 5dlu, "
						+ "right:max(40dlu;p), 4dlu, 50dlu, 5dlu,"
						+ "right:max(40dlu;p), 4dlu, 50dlu", 
						"");

		DefaultFormBuilder builder = TMTDefaultFormBuilder.getDFB(layout);

		builder.appendSeparator(resBundle.getString("date"));

		builder.append(resBundle.getString("year"), yearField);
		builder.append(resBundle.getString("month"), monthBox);
		builder.append(resBundle.getString("day"), dayBox);
		builder.append(resBundle.getString("time"), timeBox);
		builder.nextLine();

		builder.appendSeparator(resBundle.getString("place"));
		builder.nextLine();
		builder.append(resBundle.getString("identifier"), addressIdentifierBox);
		builder.nextLine();

		builder.append(resBundle.getString("street"), streetField);
		builder.append(resBundle.getString("housenumber"), housenumberField);
		builder.append(resBundle.getString("postal"), postalField);
		builder.append(resBundle.getString("city"), cityField);
		builder.nextLine();

		add(builder.getPanel(), BorderLayout.NORTH);

		pop = new ParticipantsOveriewPanel(model, controller);

		this.buttonPanel = new EventSingleButtonPanel(model, controller);

		add(buttonPanel, BorderLayout.SOUTH);
	}

	public void update() {
		Event event = model.getSelectedEvent();
		addressIdentifierBox.removeActionListener(controller);

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		if (event == null) {

			yearField.setText("");
			streetField.setText("");
			housenumberField.setText("");
			postalField.setText("");
			cityField.setText("");

			addressIdentifierBox.setSelectedIndex(0);
			dayBox.setSelectedIndex(0);
			monthBox.setSelectedIndex(0);
			timeBox.setSelectedIndex(0);

			remove(pop);

		} else {

			streetField.setText(event.getPlace().getStreet());
			housenumberField.setText("" + event.getPlace().getHousenumber());
			postalField.setText("" + event.getPlace().getPostal());
			cityField.setText(event.getPlace().getCity());

			Calendar calendar = new GregorianCalendar();
			Date dateTime = new Date(event.getDate());
			calendar.setTime(dateTime);

			int monthValue = calendar.get(Calendar.MONTH);
			int yearValue = calendar.get(Calendar.YEAR);
			int dayValue = calendar.get(Calendar.DAY_OF_MONTH) - 1;
			int timeValue = calendar.get(Calendar.HOUR_OF_DAY) - 10;

			dayBox.setSelectedIndex(dayValue);
			monthBox.setSelectedIndex(monthValue);
			timeBox.setSelectedIndex(timeValue);
			yearField.setText("" + yearValue);

			addressIdentifierBox.setSelectedItem(event.getPlace()
					.getIdentifier());

			remove(pop);
			pop = new ParticipantsOveriewPanel(model, controller);
			add(pop, BorderLayout.CENTER);
		}

		Boolean authorizated = Auth.getAuthorization(null, model);

		if (!authorizated) {
			streetField.setEditable(false);
			housenumberField.setEditable(false);
			postalField.setEditable(false);
			cityField.setEditable(false);
			yearField.setEditable(false);
			monthBox.setEnabled(false);
			dayBox.setEnabled(false);
			timeBox.setEnabled(false);
			addressIdentifierBox.setEnabled(false);
		} else {
			if (event == null) {
				streetField.setEditable(true);
				housenumberField.setEditable(true);
				postalField.setEditable(true);
				cityField.setEditable(true);
			} else {

				if (model.getSelectedEvent().getPlace().getIdentifier().equals(
						resBundle.getString("newAddress"))) {
					streetField.setEditable(true);
					housenumberField.setEditable(true);
					postalField.setEditable(true);
					cityField.setEditable(true);
				} else {
					streetField.setEditable(false);
					housenumberField.setEditable(false);
					postalField.setEditable(false);
					cityField.setEditable(false);
				}
			}

			yearField.setEditable(true);
			monthBox.setEnabled(true);
			dayBox.setEnabled(true);
			timeBox.setEnabled(true);
		}
		addressIdentifierBox.addActionListener(controller);

		remove(buttonPanel);
		this.buttonPanel = new EventSingleButtonPanel(model, controller);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public void updateAddress() {

		ResourceBundle resBundle = BundleLoader.getResourceBundle();
		Address address = model.getSelectedAddress();

		if (address == null) {
			streetField.setText("");
			housenumberField.setText("");
			postalField.setText("");
			cityField.setText("");

			streetField.setEditable(true);
			housenumberField.setEditable(true);
			postalField.setEditable(true);
			cityField.setEditable(true);

		} else {

			streetField.setText(address.getStreet());
			housenumberField.setText("" + address.getHousenumber());
			postalField.setText("" + address.getPostal());
			cityField.setText(address.getCity());

			if (address.getIdentifier().equals(
					resBundle.getString("newAddress"))) {
				streetField.setEditable(true);
				housenumberField.setEditable(true);
				postalField.setEditable(true);
				cityField.setEditable(true);
			} else {
				streetField.setEditable(false);
				housenumberField.setEditable(false);
				postalField.setEditable(false);
				cityField.setEditable(false);
			}
		}
	}

	public TMTTextField getYearField() {
		return yearField;
	}

	public TMTComboBox getMonthBox() {
		return monthBox;
	}

	public TMTComboBox getDayBox() {
		return dayBox;
	}

	public TMTComboBox getTimeBox() {
		return timeBox;
	}

	public TMTComboBox getAddressIdentifierBox() {
		return addressIdentifierBox;
	}

	public TMTTextField getStreetField() {
		return streetField;
	}

	public TMTTextField getHousenumberField() {
		return housenumberField;
	}

	public TMTTextField getPostalField() {
		return postalField;
	}

	public TMTTextField getCityField() {
		return cityField;
	}
}

class EventSingleButtonPanel extends TMTPanel {

	public EventSingleButtonPanel(Model model, Controller controller) {

		Boolean authorizated = Auth.getAuthorization(null, model);

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		TMTNormalButton backButton = new TMTNormalButton(resBundle
				.getString("back"));
		backButton.setActionCommand(Commands.GO_BACK);
		backButton.addActionListener(controller);
		add(backButton);

		Event event = model.getSelectedEvent();

		if (authorizated) {
			TMTNormalButton submitButton = new TMTNormalButton(resBundle
					.getString("submit"));
			submitButton.setActionCommand(Commands.EVENT_UPDATE);
			submitButton.addActionListener(controller);
			add(submitButton);

			if (event != null) {

				TMTNormalButton participantsButton = new TMTNormalButton(
						resBundle.getString("editParticipants"));
				participantsButton.setActionCommand(Commands.SHOW_PARTICIPANTS);
				participantsButton.addActionListener(controller);
				add(participantsButton);

				TMTNormalButton deleteButton = new TMTNormalButton(resBundle
						.getString("delete"));
				deleteButton.setActionCommand(Commands.EVENT_DELETE);
				deleteButton.addActionListener(controller);
				add(deleteButton);
			}
		}
	}
}

class ParticipantsOveriewPanel extends TMTPanel {

	public ParticipantsOveriewPanel(Model model, Controller controller) {

		ResourceBundle resBundle = BundleLoader.getResourceBundle();

		TablePanel participatingPlayersPanel = new TablePanel(model,
				controller, Commands.SHOW_PARTICIPANTS);
		participatingPlayersPanel.setBorder(new TMTTitledBorder(resBundle
				.getString("participants")));

		add(participatingPlayersPanel);
	}
}